package com.jiaoyou.baojian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

/**
 * Created by hzzhangyumin on 2016/4/23.
 */
public abstract class BaseFragment extends Fragment {
    protected void toast(String s){
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }

    protected boolean isBackground = false;
    protected boolean isInited = false;

    protected String setFragmentTitle(){
        return getClass().getName();
    };

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            isBackground = false;
        } else {
            isBackground = true;
        }
        onVisibleChange();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //viewpager中通过这个判断
        if(isVisibleToUser){
            isBackground = false;
        } else {
            isBackground = true;
        }
        onVisibleChange();
    }

    public void onVisibleChange(){
        if(!isInited && !isBackground) {
            onFirstVisible();
            isInited = true;
        }
    }

    public void onFirstVisible(){
    }

    public void startActivity(Class clazz){
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    public void startActivity(Class clazz, Bundle extras){
        Intent intent = new Intent(getActivity(), clazz);
        intent.putExtras(extras);
        startActivity(intent);
    }
}
